﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenNI;
using Mapenect.GesturePatterns;
using Mapenect.Data;

namespace Mapenect.Data
{

    class DataContainer
    {

        private Dictionary<user, Dictionary<SkeletonJoint, FixedQueueList<MyJoint>>> _userContainer;

        public DataContainer()
        {
            //Create UserContainer
            _userContainer = new Dictionary<user, Dictionary<SkeletonJoint, FixedQueueList<MyJoint>>>();

            //Create JointContainers for 4 Users
            Dictionary<SkeletonJoint, FixedQueueList<MyJoint>> _jointContainer1 = new Dictionary<SkeletonJoint,FixedQueueList<MyJoint>>();
            Dictionary<SkeletonJoint, FixedQueueList<MyJoint>> _jointContainer2 = new Dictionary<SkeletonJoint, FixedQueueList<MyJoint>>();
            Dictionary<SkeletonJoint, FixedQueueList<MyJoint>> _jointContainer3 = new Dictionary<SkeletonJoint, FixedQueueList<MyJoint>>();
            Dictionary<SkeletonJoint, FixedQueueList<MyJoint>> _jointContainer4 = new Dictionary<SkeletonJoint, FixedQueueList<MyJoint>>();

            //Add JointContainers to Specific Users
            _userContainer.Add(user.user1, _jointContainer1);
            _userContainer.Add(user.user2, _jointContainer2);
            _userContainer.Add(user.user3, _jointContainer3);
            _userContainer.Add(user.user4, _jointContainer4);
        }

        public void WriteJoint(user user, SkeletonJoint joint, SkeletonJointTransformation pos)
        { 
            //Check for Availability
            if (!_userContainer[user].ContainsKey(joint))
            {
                _userContainer[user].Add(joint, new FixedQueueList<MyJoint>(20));
            }

            //Get Last known Joint for Delta
            MyJoint lastJoint = _userContainer[user][joint].GetLastElement();

            //Add new Joint
            if (lastJoint == null)
            {
                _userContainer[user][joint].Add(new MyJoint(pos));
            }
            else
            {
                _userContainer[user][joint].Add(new MyJoint(pos, lastJoint));
            }
        }

        public FixedQueueList<MyJoint> GetJoint(user user, SkeletonJoint joint)
        {
            if(_userContainer[user].ContainsKey(joint))
            {
                return _userContainer[user][joint];
            }
            return null;
        }

        public Dictionary<SkeletonJoint, FixedQueueList<MyJoint>> getUserData(user usr)
        {
            if (_userContainer == null)
                throw new NullReferenceException("_userContainer noch nicht angelegt.");

            return _userContainer[usr];
        }
    }
}
